

const tools = require('../util/tool');
module.exports = class poster_sql extends require('./mysql'){


    /**
     * 获取公告列表
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getPosterDataSql(currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(currPage * pageNum);
        return new Promise((resolve , reject)=>{
            let sql = "select * from `poster` order by createtime desc limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }
    /**
     * 公告列表总数
     * @returns {Promise<any>}
     */
    static getPosterDataTotal(){
        return new Promise((resolve, reject) => {
            let sql = "select count(1) as count from `poster`"
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 发布公告
     * @param communityname
     * @param title
     * @param content
     * @returns {Promise<any>}
     */
    static addPosterSql(communityname,title,content){

        return new Promise((resolve, reject) => {
            let sql = "insert into `poster`(communityname,title,content,createtime) values (?, ?, ?, ?)";
            let createtime = tools.formatDate()
            // let value = [u_id,username,password,type,email,nickname,sex,address,note,createtime];
            console.log(sql)
            this.query(sql,this.formarParams(communityname,title,content,createtime)).then(result=>{
                resolve('发布成功')
            }).catch(err=>{
                reject('发布失败')
            })
        })
    }


    /**
     * 根据p_id删除公告
     * @param p_id
     * @returns {Promise<any>}
     */
    static delPosterSql(p_id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from poster where p_id ='+p_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除公告信息成功')
            }).catch(err=>{
                reject('删除公告信息失败')
            })
        })
    }

    /**
     * 维修列表获取
     * @param homename
     * @param housename
     * @param resultstatus
     * @param startDate
     * @param endDate
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getRepairDataSql(repairtype,homename,resultstatus,startDate,endDate,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `repair` where 1=1";
            if (repairtype) sql += " and repairtype= '" + repairtype + "'";
            if (homename) sql += " and homename like '%"+homename+"%'";
            if (resultstatus) sql += " and resultstatus= '" + resultstatus + "'";
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            sql += " limit ?,?";

            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                console.log(this.formarParams(currPage,pageNum));
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 维修列表总数
     * @param homename
     * @param housename
     * @param resultstatus
     * @param startDate
     * @param endDate
     * @returns {Promise<any>}
     */
    static getRepairDataTotal(repairtype,homename,resultstatus,startDate,endDate){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `repair` where 1=1';
            if (repairtype) sql += " and repairtype= '" + repairtype + "'";
            if (homename) sql += " and homename like '%"+homename+"%'";
            if (resultstatus) sql += " and resultstatus= '" + resultstatus + "'";
            if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    // /**
    //  * 维修列表获取
    //  * @param homename
    //  * @param housename
    //  * @param resultstatus
    //  * @param startDate
    //  * @param endDate
    //  * @param currPage
    //  * @param pageNum
    //  * @returns {Promise<any>}
    //  */
    // static getRepairHouseSql(homename,repairstatus,startDate,endDate,currPage,pageNum){
    //     pageNum = Number(pageNum);
    //     currPage = Number(currPage);
    //     currPage = Number(pageNum * currPage);
    //     return new Promise((resolve, reject)=>{
    //         // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
    //         let sql="select * from `repair` where 1=1";
    //         if (homename) sql += " and homename like '%"+homename+"%'";
    //         if (repairstatus) sql += " and resultstatus= '" + repairstatus + "'";
    //         if (startDate && endDate) sql += " and (devicetime BETWEEN '"+startDate+"' AND '"+endDate+"')";
    //         sql += " limit ?,?";
    //
    //         console.log(sql);
    //         this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
    //             console.log(this.formarParams(currPage,pageNum));
    //             resolve(result)
    //         }).catch(err=>{
    //             reject(err)
    //         })
    //     })
    // }
    //
    // /**
    //  * 维修列表总数
    //  * @param homename
    //  * @param housename
    //  * @param resultstatus
    //  * @param startDate
    //  * @param endDate
    //  * @returns {Promise<any>}
    //  */
    // static getRepairHouseTotal(homename,repairstatus,startDate,endDate){
    //     return new Promise((resolve, reject)=>{
    //         // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
    //         let sql='select count(1) as count from `repair` where 1=1';
    //         if (homename) sql += " and homename like '%"+homename+"%'";
    //         if (repairstatus) sql += " and repairstatus= '" + repairstatus + "'";
    //         if (startDate && endDate) sql += " and (createtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
    //         console.log(sql);
    //         this.query(sql).then(result=>{
    //             resolve(result)
    //         }).catch(err=>{
    //             reject(err)
    //         })
    //     })
    // }


    /**
     * 删除维修信息
     * @param r_id
     * @returns {Promise<any>}
     */
    static delRepairSql(r_id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from `repair` where r_id ='+r_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除维修信息成功')
            }).catch(err=>{
                reject('删除维修信息失败')
            })
        })
    }


    /**
     * 智能列表处理
     * @param r_id
     * @param homename
     * @param housename
     * @param repaircontent
     * @param resultcontent
     * @param resultstatus
     * @returns {Promise<any>}
     */
    static updateSmartRepairSql(r_id,homename,housename,repaircontent,resultcontent,resultstatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `repair` set homename="'+homename+'",housename="'+housename+'",repaircontent="'+repaircontent+'",repaircontent="'+repaircontent+'",resultcontent="'+resultcontent+'",resultstatus="'+resultstatus+'" where r_id ='+r_id
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }

    /**
     * 用户申请维修
     * @param homename
     * @param housename
     * @param repairtype
     * @param repaircontent
     * @returns {Promise<any>}
     */
    static addRepairSql(homename,housename,repairtype,repaircontent,h_id){
        return new Promise((resolve, reject) => {
            let sql = "insert into `repair`(homename,housename,repairtype,repaircontent,h_id,createtime) values (?, ?, ?, ?, ?, ?)";
            let createtime = tools.formatDate();
            // let value = [u_id,username,password,type,email,nickname,sex,address,note,createtime];
            console.log(sql);
            this.query(sql,this.formarParams(homename,housename,repairtype,repaircontent,h_id,createtime)).then(result=>{
                resolve('申请成功')
            }).catch(err=>{
                reject('申请失败')
            })
        })
    }


    // /**
    //  * 家居列表处理
    //  * @param r_id
    //  * @param homename
    //  * @param housename
    //  * @param housetext
    //  * @param houseresult
    //  * @param repairstatus
    //  * @returns {Promise<any>}
    //  */
    // static updateHouseRepairSql(r_id,homename,housename,housetext,houseresult,repairstatus){
    //     let currTime = tools.formatDate()
    //     return new Promise((resolve, reject) => {
    //         let sql = 'update `repair` set homename="'+homename+'",housename="'+housename+'",housetext="'+housetext+'",housetext="'+housetext+'",houseresult="'+houseresult+'",repairstatus="'+repairstatus+'", devicetime="'+currTime+'" where r_id ='+r_id
    //         console.log(sql);
    //         this.query(sql).then(result=>{
    //             resolve('更新成功')
    //         }).catch(err=>{
    //             reject('更新失败')
    //         })
    //     })
    // }


    /**
     * 反馈列表分页获取
     * @param username
     * @param feedtext
     * @param startDate
     * @param endDate
     * @param feedresult
     * @param feedstatus
     * @param currPage
     * @param pageNum
     * @returns {Promise<any>}
     */
    static getFeedBackSql(username,feedtext,startDate,endDate,feedresult,feedstatus,currPage,pageNum){
        pageNum = Number(pageNum);
        currPage = Number(currPage);
        currPage = Number(pageNum * currPage);
        return new Promise((resolve, reject)=>{
            // let sql="select * from `user` where username like '%"+username+"%' and type="+type+" limit ?,?"
            let sql="select * from `feedback` where 1=1";
            if (username) sql += " and username like '%"+username+"%'";
            if (feedtext) sql += " and feedtext like '%"+feedtext+"%'";
            if (startDate && endDate) sql += " and (feedtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            if (feedresult) sql += " and feedresult like '%"+feedresult+"%'";
            if (feedstatus) sql += " and feedstatus= '" + feedstatus + "'";
            sql += " limit ?,?";
            console.log(sql);
            this.query(sql,this.formarParams(currPage,pageNum)).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }

    /**
     * 反馈列表总数获取
     * @param username
     * @param feedtext
     * @param startDate
     * @param endDate
     * @param feedresult
     * @param feedstatus
     * @returns {Promise<any>}
     */
    static getFeedBackTotal(username,feedtext,startDate,endDate,feedresult,feedstatus){
        return new Promise((resolve, reject)=>{
            // let sql="select count(1) as count from `user` where username like '%"+username+"%' and type="+type+" "
            let sql='select count(1) as count from `feedback` where 1=1';
            if (username) sql += " and username like '%"+username+"%'";
            if (feedtext) sql += " and feedtext like '%"+feedtext+"%'";
            if (startDate && endDate) sql += " and (feedtime BETWEEN '"+startDate+"' AND '"+endDate+"')";
            if (feedresult) sql += " and feedresult like '%"+feedresult+"%'";
            if (feedstatus) sql += " and feedstatus= '" + feedstatus + "'";
            console.log(sql);
            this.query(sql).then(result=>{
                resolve(result)
            }).catch(err=>{
                reject(err)
            })
        })
    }


    /**
     * 更新评分
     * @param f_id
     * @param feedscore
     * @returns {Promise<any>}
     */
    static updateScoreSql(f_id,feedscore){
        feedscore = Number(feedscore);
        return new Promise((resolve, reject) => {
            let sql = 'update `feedback` set feedscore="'+feedscore+'" where f_id ='+f_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('评分成功')
            }).catch(err=>{
                reject('评分失败')
            })
        })
    }

    /**
     * 根据f_id删除反馈内容
     * @param f_id
     * @returns {Promise<any>}
     */
    static delFeedbackSql(f_id){
        return new Promise((resolve, reject) => {
            let sql = 'delete from `feedback` where f_id ='+f_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('删除反馈信息成功')
            }).catch(err=>{
                reject('删除反馈信息失败')
            })
        })
    }


    /**
     * 反馈批量删除
     * @param ids
     * @returns {Promise<any>}
     */
    static delFeedbackDataSql(ids){
        return new Promise((resolve, reject) => {
            let sql = `delete from feedback where f_id in (${ids})` ; // in 是用来做批量删除的写法
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('批量删除反馈内容成功')
            }).catch(err=>{
                reject('批量删除反馈内容成功')
            })
        })
    }


    /**
     * 反馈处理
     * @param f_id
     * @param username
     * @param feedtext
     * @param feedtime
     * @param feedresult
     * @param feedstatus
     * @returns {Promise<any>}
     */
    static updateFeedbackSql(f_id,feedresult,feedstatus){
        return new Promise((resolve, reject) => {
            let sql = 'update `feedback` set feedresult="'+feedresult+'",feedstatus="'+feedstatus+'" where f_id ='+f_id;
            console.log(sql);
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }


    /**
     * 用户提交反馈
     * @param username
     * @param feedtext
     * @returns {Promise<any>}
     */
    static addFeedBackSql(username,feedtext){
        return new Promise((resolve, reject) => {
            let sql = "insert into `feedback`(username,feedtext,feedtime) values (?, ?, ?)";
            let feedtime = tools.formatDate();
            // let value = [u_id,username,password,type,email,nickname,sex,address,note,createtime];
            console.log(sql);
            this.query(sql,this.formarParams(username,feedtext,feedtime)).then(result=>{
                resolve('反馈成功')
            }).catch(err=>{
                reject('反馈失败')
            })
        })
    }


    /**
     * 更新公告阅读量
     * @param p_id
     * @param postnum
     * @returns {Promise<any>}
     */
    static updatePostNumSql(p_id,postnum){
        return new Promise((resolve, reject) => {
            let sql = 'update `poster` set postnum="'+postnum+'" where p_id ='+p_id;
            console.log(sql)
            this.query(sql).then(result=>{
                resolve('更新成功')
            }).catch(err=>{
                reject('更新失败')
            })
        })
    }

    
}